package com.dhq.myapp.util

import android.app.Activity
import android.os.Build
import android.util.Log
import android.util.TypedValue
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.annotation.*
import androidx.core.content.ContextCompat
import com.dhq.myapp.R


/**
 * DESC
 * Created by admin on 2016/12/2.
 */
class HeaderUtil(private val activity: Activity, private var rootView: View? = null) {

    companion object {
        private const val TAG = "HeaderUtil"
    }

    private var id_header_bg: RelativeLayout? = null
    private var LeftIv: ImageView? = null
    private var left_tv: TextView? = null
    private var title_tv: TextView? = null
    private var right_iv: ImageView? = null
    private var right_tv: TextView? = null

    init {
        rootView?.let { initView() }
    }

    constructor(activity: Activity) : this(activity, null)

    fun setCustomHeaderView(rootView: View, headerListener: CustomHeaderListener): HeaderUtil {
        this.rootView = rootView
        val customHeaderHolder = CustomHeaderHolder(rootView)
        headerListener.convert(customHeaderHolder)
        return this
    }

    private fun initView() {
        id_header_bg = rootView?.findViewById(R.id.id_header)
        if (id_header_bg == null) {
            return
        }

        LeftIv = rootView?.findViewById(R.id.iv_header_left)
        title_tv = rootView?.findViewById(R.id.tv_header_title)
        left_tv = rootView?.findViewById(R.id.tv_header_left)
        right_iv = rootView?.findViewById(R.id.iv_header_right)
        right_tv = rootView?.findViewById(R.id.tv_header_right)

        setStatusBarHeight(R.id.id_header_relative_layout)
        setLeftBack()
    }

    fun setStatusBarHeight(viewId: Int) {
        if (rootView != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            val relativeLayout = rootView?.findViewById<RelativeLayout>(viewId)
            relativeLayout?.let { layout ->
                val layoutParams = layout.layoutParams as RelativeLayout.LayoutParams

                Log.e("设置高度",getStatusBarHeight().toString())

                layoutParams.setMargins(0, getStatusBarHeight(), 0, 0)
                layout.layoutParams = layoutParams
            }
        }
    }

    /**
     * 获取状态栏高度
     * @return 状态栏高度
     */
    private fun getStatusBarHeight(): Int {
        val resourceId = activity.resources.getIdentifier("status_bar_height", "dimen", "android")
        return activity.resources.getDimensionPixelSize(resourceId)
    }

    /**
     * 隐藏左侧返回按钮
     */
    fun hintLeftBack(): HeaderUtil {
        LeftIv?.visibility = View.INVISIBLE
        return this
    }

    /**
     * 显示左侧返回按钮
     */
    fun setLeftBack(): HeaderUtil {
        return setLeftBack(View.OnClickListener {
            activity.onBackPressed()
        })
    }

    /**
     * 显示左侧返回按钮
     */
    fun setLeftBack(clickListener: View.OnClickListener): HeaderUtil {
        return setLeftBack(0, clickListener)
    }

    /**
     * 显示左侧返回按钮
     */
    fun setLeftBack(serid: Int, clickListener: View.OnClickListener): HeaderUtil {
        left_tv?.visibility = View.GONE
        LeftIv?.visibility = View.VISIBLE
        if (serid != 0) {
            LeftIv?.setImageResource(serid)
        }
        LeftIv?.setOnClickListener(clickListener)
        return this
    }

    /**
     * 设置中间标题
     * @param title 文字标题
     */
    fun setHeaderTitle(title: String): HeaderUtil {
        title_tv?.visibility = View.VISIBLE
        title_tv?.text = title
        return this
    }

    /**
     * 设置中间标题字体大小
     * @param textSize 文字字体大小资源
     */
    fun setHeaderTitleSize(@DimenRes textSize: Int): HeaderUtil {
        title_tv?.setTextSize(
            TypedValue.COMPLEX_UNIT_PX,
            activity.resources.getDimensionPixelSize(textSize).toFloat()
        )
        return this
    }

    /**
     * 设置中间标题字体颜色
     * @param title 文字颜色资源
     */
    fun setHeaderTitleColor(@ColorRes title: Int): HeaderUtil {
        title_tv?.setTextColor(ContextCompat.getColor(activity, title))
        return this
    }

    /**
     * 设置中间标题
     * @param resId 资源id
     */
    fun setHeaderTitle(@StringRes resId: Int): HeaderUtil {
        title_tv?.visibility = View.VISIBLE
        title_tv?.setText(resId)
        return this
    }

    /**
     * 设置左侧图片
     * @param resId 资源id
     * @param onClickListener 点击事件
     */
    fun setHeaderLeftImage(resId: Int, onClickListener: View.OnClickListener?): HeaderUtil {
        left_tv?.visibility = View.GONE
        LeftIv?.visibility = View.VISIBLE
        LeftIv?.setImageResource(resId)
        onClickListener?.let { LeftIv?.setOnClickListener(it) }
        return this
    }

    /**
     * 设置左侧点击事件
     * @param onClickListener 点击事件
     */
    fun setHeaderLeftListener(onClickListener: View.OnClickListener?): HeaderUtil {
        left_tv?.visibility = View.GONE
        LeftIv?.visibility = View.VISIBLE
        onClickListener?.let { LeftIv?.setOnClickListener(it) }
        return this
    }

    /**
     * 设置右侧图片
     * @param resId 资源id
     * @param onClickListener 点击事件
     */
    fun setHeaderRightImage(resId: Int, onClickListener: View.OnClickListener?): HeaderUtil {
        right_tv?.visibility = View.GONE
        right_iv?.visibility = View.VISIBLE
        right_iv?.setImageResource(resId)
        onClickListener?.let { right_iv?.setOnClickListener(it) }
        return this
    }

    /**
     * 设置左侧文字
     * @param str 文字
     * @param onClickListener 点击事件
     */
    fun setHeaderLeftText(str: String, onClickListener: View.OnClickListener?): HeaderUtil {
        LeftIv?.visibility = View.GONE
        left_tv?.visibility = View.VISIBLE
        left_tv?.text = str
        onClickListener?.let { left_tv?.setOnClickListener(it) }
        return this
    }

    /**
     * 设置左侧文字大小
     * @param strSizeId 文字大小资源
     */
    fun setHeaderLeftTextSize(@DimenRes strSizeId: Int): HeaderUtil {
        val textSize = activity.resources.getDimensionPixelSize(strSizeId)
        left_tv?.textSize = textSize.toFloat()
        return this
    }

    /**
     * 设置左侧文字颜色
     * @param colorId 文字颜色资源
     */
    fun setHeaderLeftTextColor(@ColorRes colorId: Int): HeaderUtil {
        val color = ContextCompat.getColor(activity, colorId)
        left_tv?.setTextColor(color)
        return this
    }

    /**
     * 设置右侧文字大小
     * @param strSizeId 文字大小资源
     */
    fun setHeaderRightTextSize(@DimenRes strSizeId: Int): HeaderUtil {
        val textSize = activity.resources.getDimensionPixelSize(strSizeId)
        right_tv?.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize.toFloat())
        return this
    }

    /**
     * 设置右侧文字颜色
     * @param colorId 文字颜色资源
     */
    fun setHeaderRightTextColor(@ColorRes colorId: Int): HeaderUtil {
        val color = ContextCompat.getColor(activity, colorId)
        right_tv?.setTextColor(color)
        return this
    }

    /**
     * 设置右侧文字
     * @param str 文字
     * @param onClickListener 点击事件
     */
    fun setHeaderRightText(str: String, onClickListener: View.OnClickListener?): HeaderUtil {
        right_iv?.visibility = View.GONE
        right_tv?.visibility = View.VISIBLE
        right_tv?.text = str
        onClickListener?.let { right_tv?.setOnClickListener(it) }
        return this
    }

    /**
     * 设置右侧文字
     * @param resId 资源id
     * @param onClickListener 点击事件
     */
    fun setHeaderRightText(@StringRes resId: Int, onClickListener: View.OnClickListener?): HeaderUtil {
        right_iv?.visibility = View.GONE
        right_tv?.visibility = View.VISIBLE
        right_tv?.setText(resId)
        onClickListener?.let { right_tv?.setOnClickListener(it) }
        return this
    }

    /**
     * 设置右侧文字
     * @param str 文字
     */
    fun setHeaderRightText(str: String): HeaderUtil {
        right_iv?.visibility = View.GONE
        right_tv?.visibility = View.VISIBLE
        right_tv?.text = str
        return this
    }

    /**
     * 设置头部背景
     * @param serid 背景资源id
     */
    fun setHeaderBg(serid: Int): HeaderUtil {
        id_header_bg?.setBackgroundResource(serid)
        return this
    }

    /**
     * 隐藏头部
     */
    fun setHeaderHint(): HeaderUtil {
        id_header_bg?.visibility = View.GONE
        return this
    }
}